package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.common.config.Const;
import com.jieshuibao.mapper.AdvisoryMapper;
import com.jieshuibao.mapper.AdvisoryRecordingMapper;
import com.jieshuibao.mapper.AdvisoryUserMapper;
import com.jieshuibao.mapper.CompanyMapper;
import com.jieshuibao.mapper.CounselorMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Advisory;
import com.jieshuibao.model.db.AdvisoryExample;
import com.jieshuibao.model.db.Counselor;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class AdvisoryService{
	
	//private static final String appId = "8aaf07086619653001661b1865b301fa";
	
	@Resource
	private AdvisoryMapper advisoryMapper;
	
	@Resource
	private AdvisoryUserMapper advisoryUserMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private CompanyMapper companyMapper;
	
	@Resource
	private AdvisoryRecordingMapper advisoryRecordingMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper; 
	
	@Resource
	private CounselorMapper counselorMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	@Resource
	private SmsSendService smsSendService;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private PushService pushService;
	
	@Resource
	private UserDealRecordService userDealRecordService;
	
	
	public void addAdvisory(Advisory advisory) {
		advisoryMapper.insertSelective(advisory);
	}
	
	public void delAdvisoryByUuid(String uuid) {
		advisoryMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateAdvisory(Advisory advisory) {
		advisoryMapper.updateByPrimaryKeySelective(advisory);
	}
	
	public Advisory getAdvisoryByUuid(String uuid) {
		return advisoryMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Advisory> getPageAdvisory(AdvisoryExample advisoryExample) {
		return advisoryMapper.selectPage(advisoryExample);
	}
	
	public int countAdvisory(AdvisoryExample advisoryExample) {
		return advisoryMapper.countByExample(advisoryExample);
	}
	
	
	public List<Advisory> getAdvisoryByExample(AdvisoryExample advisoryExample){
		return advisoryMapper.selectByExample(advisoryExample);
	}
	
	public void updateAdvisoryAndCounselor(Advisory advisory,Counselor counselor){
		if(advisory!=null){
			advisoryMapper.updateByPrimaryKeySelective(advisory);
		}
		if(counselor!=null){
			counselorMapper.updateByPrimaryKeySelective(counselor);
		}
	}
	/**
	 * 一对一咨询支付
	 * @param productId
	 * @param userId
	 * @param counId
	 * @param money
	 * @param payType
	 * @return
	 */
	public boolean updateAdvisoryBuyVersionTwo(String productId,String userId,String counId,int money,String payType ){
		try {
			Advisory advisory = new Advisory();
			advisory.setUuid(productId);
			advisory.setUserRes(counId);
			advisory.setUserAsk(userId);
			advisory.setMoney(money);
			int compareTo = userId.compareTo(counId);
			String phone=compareTo>=0?userId+"_"+counId:counId+"_"+userId;
			advisory.setOnlyId(phone);
			long currentTimeMillis = System.currentTimeMillis();
			
			advisory.setStartTime(currentTimeMillis);
			advisory.setType(Const.ADVISORY_STATUS_0);
			advisory.setScore(5);
			advisory.setAppraisal("暂无评价内容");
			
			String userDealRecodType="";
			if (payType.equals("balance")) {
				advisory.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				advisory.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecodType=PayTypeUtil.PAYTYPE_BALANCE;
			}else if (payType.equals("VipBalance")) {
				advisory.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
				advisory.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecodType=PayTypeUtil.PAYTYPE_VIPBALANCE;
			}else if (payType.equals("weixin")) {
				advisory.setPayType(PayTypeUtil.PAYTYPE_WEIXIN);
				advisory.setPayStatus(PayCommonUtil.PAYPENDING_STATUS);
			}
			advisoryMapper.insertSelective(advisory);
			if (payType.equals("balance")||payType.equals("VipBalance")) {
				userDealRecordService.updateAdviosyRunning(userId, productId, userDealRecodType, money);
			}
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
}
